package no.cicero.testframework.reporting;

import java.io.IOException;

public class ReportingUseCase {

	private String useCaseName;
	private String useCaseDescription;
	private String useCaseShortDescription;
	private ReportingTestCase currentTestCase = null;
	private Reporting reporting;
	private int failedStepsCount = 0;
	private int totalStepsCount = 0;

	public ReportingUseCase(String useCaseName, String useCaseDescription, String useCaseShortDescription, Reporting reporting) {
		this.useCaseName = useCaseName;
		this.useCaseDescription = useCaseDescription;
		this.useCaseShortDescription = useCaseShortDescription;
		this.reporting = reporting;
	}

	public String getUseCaseName() { return useCaseName; }
	public String getUseCaseShortDescription() { return useCaseShortDescription; }
	public String getUseCaseDescription() { return useCaseDescription; }
	public ReportingTestCase getCurrentTestCase() { return currentTestCase; }
	public int getTotalStepsCount() { return totalStepsCount; }
	public int getFailedStepsCount() { return failedStepsCount; }

	/**
	 * This section implements all methods needed to work
	 * with Test Cases
	 */
	public ReportingTestCase testCaseStart(String testCaseName, String testCaseDescription) throws Exception {
		currentTestCase = new ReportingTestCase(testCaseName, testCaseDescription, reporting);
		reporting.notifyReports(new Reporting.Message<IReport>() {
			public void send(IReport obj) throws IOException {
				obj.testCaseStarted(currentTestCase);
			}
		});
		return currentTestCase;
	}

	public void testCaseSucceed() {
		reporting.notifyReportsNoThrow(new Reporting.MessageNoThrow<IReport>() {
			public void send(IReport obj) {
				obj.testCaseSucceeded(currentTestCase);
			}
		});
		testCaseEnd();
	}

	public void testCaseFailed(final Exception ex) {
		reporting.notifyReportsNoThrow(new Reporting.MessageNoThrow<IReport>() {
			public void send(IReport obj) {
				obj.testCaseFailed(currentTestCase, ex);
			}
		});
		testCaseEnd();
	}

	private void testCaseEnd() {
		totalStepsCount += currentTestCase.getTotalStepsCount();
		failedStepsCount += currentTestCase.getFailedStepsCount();
		currentTestCase = null;
	}
}
